/**
* @mxmlc -output=bin/assets/modules/infomation.swf -external-library-path+=lib/library.swc -debug=false -noplay
*/
package infomation 
{
	import flash.display.DisplayObject;
	import infomation.view.ActionInfo;
	import infomation.view.CityInfo1;
	import infomation.view.CityInfo2;
	import infomation.view.CityInfo3;
	import infomation.view.CityInfo4;
	import infomation.view.OrderInfo;
	import infomation.view.TimeInfo;
	import org.airmvc.View;
	import vo.CastleVO;
	
	/**
	 * ...
	 * @author WLDragon 2014-02-28
	 */
	public class InfomationV extends View 
	{
		
		public function InfomationV(){}
		
		override public function init():void 
		{			
			cityInfo1 = new CityInfo1();
			cityInfo1.visible = false;
			cityInfo2 = new CityInfo2();
			cityInfo2.visible = false;
			cityInfo3 = new CityInfo3();
			cityInfo3.visible = false;
			cityInfo4 = new CityInfo4();
			cityInfo4.visible = false;
			
			addChild(cityInfo1);
			addChild(cityInfo2);
			addChild(cityInfo3);
			addChild(cityInfo4);
			
			timeInfo = new TimeInfo();
			timeInfo.update();
			addChild(timeInfo);
			
			orderInfo = new OrderInfo();
			orderInfo.update();
			addChild(orderInfo);
			
			actionInfo = new ActionInfo();
			addChild(actionInfo);
			
			Lyr.ui.addChild(this);
		}
		
		override protected function addListeners():void 
		{
			receive(InfomationC.SHOW_OR_HIDE,handleShowOrHide);
			receive(InfomationC.OPEN_CITY_INFO, handleOpenCityInfo);
			receive(InfomationC.UPDATE_CITY_INFO, handleUpdateCityInfo);
			receive(InfomationC.UPDATE_TIME, updateTime);
			receive(InfomationC.UPDATE_ORDER, updateOrder);
			receive(InfomationC.UPDATE_ACTION, updateAction);
		}
		
		private function updateAction(date:int,gold:int,rice:int,step:int):void 
		{
			if (date == 0)
			{
				actionInfo.clearTime();
				actionInfo.visible = false;
			}
			else
			{
				if (this.visible == false)
				{
					orderInfo.visible = false;
					timeInfo.visible = false;
					cityInfo2.visible = false;
					this.visible = true;
				}
				actionInfo.update(date,gold,rice,step);
			}
		}
		
		private function updateTime(type:int):void
		{
			if (type == 1)
				timeInfo.update();
			else if (type == 2)
			{
				timeInfo.visible = true;
			}
			else
				timeInfo.visible = false;
		}
		
		private function updateOrder(type:int):void
		{
			if (type == 1)
				orderInfo.update();
			else if (type == 2)
			{
				orderInfo.visible = true;
			}
			else
				orderInfo.visible = false;
		}
		
		private function handleShowOrHide(isShow:Boolean):void 
		{
			this.visible = isShow;
		}
		
		private function handleUpdateCityInfo(castle:CastleVO,tx:int,ty:int,soliders:int):void 
		{
			cityInfo1.update(castle,tx,ty);
			cityInfo2.update(castle,tx,ty,soliders);
			cityInfo3.update(castle,tx,ty,soliders);
			cityInfo4.update(castle, tx, ty);
		}

		private function handleOpenCityInfo(type:int):void 
		{
			if (type == 0)
			{
				if(curCityInfo)
					curCityInfo.visible = false;
					
				curCityInfo = null;
				return;
			}
			
			if (this['cityInfo' + type] != curCityInfo)
			{
				if(curCityInfo)
					curCityInfo.visible = false;
					
				curCityInfo = this['cityInfo' + type];
				curCityInfo.visible = true;
			}
		}
		
		private var cityInfo1:CityInfo1;
		private var cityInfo2:CityInfo2;
		private var cityInfo3:CityInfo3;
		private var cityInfo4:CityInfo4;
		private var curCityInfo:DisplayObject;
		
		private var timeInfo:TimeInfo;
		private var orderInfo:OrderInfo;
		private var actionInfo:ActionInfo;
	}
	
}